package Robots;

import java.util.ArrayList;

import Main.Game;
import Other.Field;
import Units.Robot;
import Units.Unit;
import Units.UnitName;

import jgame.JGObject;
import jgame.JGPoint;
import jgame.JGTimer;

public class Robot3 extends Robot {
	public static int imageHeight = 89, imageWidth = 73;
	
	private final static String stringStandingDown = "robot3_down",
								stringGoDown	   = "robot1animgo_down",
								stringStopDown	   = "robot1animstop_down",
								stringMoveDown	   = "robot1animmove_down";
	
	private int state = 0;
	
	public Robot3(String name, boolean unique, double posX, double posY, int cid){
		super(name, unique, posX, posY, cid, stringStandingDown);
	}
	
	// move methods
	@Override
	public boolean canMove(Field field) {
		if (!Game.board.inBoard(field.i, field.j)) return false;
		if ( UnitName.isRobot(Game.board.getUnitName(field.i, field.j)) ) return false;
		
		boolean can = false;
		for (int i=0; i<Field.adjacentFields.length; i++) {
			Field firstMove = new Field(this.getField().i + Field.adjacentFields[i][0], 
										this.getField().j + Field.adjacentFields[i][1]);
			
			if (field.i==firstMove.i && field.j==firstMove.j) can = true; 
			
			for (int j=-1; j<2; j++){
				int nextDirection = (i+j+Field.adjacentFields.length)%Field.adjacentFields.length;
				Field secondMove = new Field(firstMove.i + Field.adjacentFields[nextDirection][0], 
											 firstMove.j + Field.adjacentFields[nextDirection][1]);
				if (field.i == secondMove.i && field.j == secondMove.j) can = true; 
			}
		}
		return can;
	}
	
	public void move() {
		switch (getState()){
			case 0:
				setGraphic(stringStandingDown);
				break;
			case 1:
				setGraphic(stringGoDown);
				break;
			case 2:
				setGraphic(stringMoveDown);
				JGPoint p = Game.board.getRealCoordinates(getTargetField());
				
				if (Game.board.distance(p, new JGPoint((int)getLastX() + this.imageWidth/2, (int)getLastY() + this.imageHeight/2)) < 5){
					xspeed = yspeed = 0;
					setState(3);
					new JGTimer(5, true){

						@Override
						public void alarm() {
							setState(0);
							Game.board.setAnimation(false);
							Field f = Robot3.this.getField();
							Field t = Robot3.this.getTargetField();
							
							Game.board.moveFromTo(f, t);
							Robot3.this.setField(Robot3.this.getTargetField());
						}
					};
				}
				break;
			case 3:
				setGraphic(stringStopDown);
				break;
			default: setGraphic(stringStandingDown);
		}		
	}

	@Override
	public ArrayList<Field> whereAttack(Field field) {
		// TODO Auto-generated method stub
		//return null;
		ArrayList<Field> ret = new ArrayList<Field>();
		ret.clear();
		return ret;
	}
	
	@Override
	public boolean canAttack(Field field) {
		// TODO Auto-generated method stub
		return false;
	}
}
